package com.humh.library.model;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 图书库存管理类

public class InventoryManagement {
  private Map<Book, Integer> inventory; // 图书及其库存数量



  public InventoryManagement() {
    this.inventory = new HashMap<>();
  }

  // 增加图书库存
  public void addBookStock(Book book, int quantity) {
    inventory.put(book, inventory.getOrDefault(book, 0) + quantity);
    System.out.println(quantity + " copies of " + book.getTitle() + " added. Total: " + inventory.get(book));
  }

  // 减少图书库存（借书）
  public boolean reduceBookStock(Book book) {
    if (inventory.containsKey(book) && inventory.get(book) > 0) {
      inventory.put(book, inventory.get(book) - 1);
      System.out.println("One copy of " + book.getTitle() + " borrowed. Remaining: " + inventory.get(book));
      return true;
    } else {
      System.out.println("No available copies of " + book.getTitle() + " to borrow.");
      return false;
    }
  }

  // 增加库存（归还图书）
  public void returnBookStock(Book book) {
    if (inventory.containsKey(book)) {
      inventory.put(book, inventory.get(book) + 1);
      System.out.println("One copy of " + book.getTitle() + " returned. Available: " + inventory.get(book));
    } else {
      System.out.println("Book not found in inventory.");
    }
  }

  // 检查库存是否有足够图书库存量
  public boolean isBookAvailable(Book book) {
    return inventory.containsKey(book) && inventory.get(book) > 0;
  }

  /**
   * 查询所有图书
   * @return
   */
  public List<Book>  findBooks(){
    return   new ArrayList<Book>(inventory.keySet());
  }

  /**
   * 根据 ISBN 查找图书
   * @param isbn 图书的 ISBN
   * @return 找到的图书对象，如果没有找到则返回 null
   */
  public Book findBookByISBN(String isbn) {

    for (Book book : inventory.keySet()) {
      if (book.getIsbn().equals(isbn)) {
        return book;
      }
    }
   return  null;
  }


}
